package com.atguigu.guli.service.base.handler;

import com.atguigu.guli.service.base.exception.GuliException;
import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.base.result.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(value = GuliException.class)
    public R exception(GuliException e){
        log.error(ExceptionUtils.getStackTrace(e));
        return R.error().message(e.getMessage()).code(e.getCodeEnum().getCode());
    }

    //最大异常
    @ExceptionHandler
    public R exception(Exception e){
        log.error("{}", ExceptionUtils.getStackTrace(e));
        return R.error();
    }
    @ExceptionHandler(value = ArithmeticException.class)
    public R exception(ArithmeticException e) {//形参必须和异常处理器注解处理异常类型一样
        log.error("{}", ExceptionUtils.getStackTrace(e));
        return R.setResult(ResultCodeEnum.DIV_ZERO_ERROR);
    }
}
